import org.apache.poi.xssf.usermodel.XSSFSheet;
import org.apache.poi.xwpf.usermodel.XWPFDocument;
import org.apache.poi.xwpf.usermodel.XWPFTable;

import javax.swing.*;
import javax.swing.plaf.FontUIResource;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.List;
import java.util.Objects;

public class WindowThreeListener implements ActionListener {
    WindowTwo windowTwo;
    int i = 5;
    int j = 4;
    public void setWindowTwo(WindowTwo windowTwo){
        this.windowTwo =windowTwo;
    }
    void initTwo() {
        UIManager.put("OptionPane.buttonFont", new FontUIResource(new Font("宋体", Font.BOLD, 12)));
        UIManager.put("Button.background", Color.white);
        UIManager.put("Button.borderPainted", false);
        UIManager.put("OptionPane.messageFont", new FontUIResource(new Font("宋体", Font.BOLD, 14)));
        JOptionPane.showMessageDialog(null, "分数填写错误，请重新填写！", "错误提醒", JOptionPane.ERROR_MESSAGE);
    }
    void initThree() {
        UIManager.put("OptionPane.buttonFont", new FontUIResource(new Font("宋体", Font.BOLD, 12)));
        UIManager.put("Button.background", Color.white);
        UIManager.put("Button.borderPainted", false);
        UIManager.put("OptionPane.messageFont", new FontUIResource(new Font("宋体", Font.BOLD, 14)));
        JOptionPane.showMessageDialog(null, "该学生实验报告中姓名或学号填写错误错误，请检查！", "错误提醒", JOptionPane.ERROR_MESSAGE);
    }
    void initFour() {
        UIManager.put("OptionPane.buttonFont", new FontUIResource(new Font("宋体", Font.BOLD, 12)));
        UIManager.put("Button.background", Color.white);
        UIManager.put("Button.borderPainted", false);
        UIManager.put("OptionPane.messageFont", new FontUIResource(new Font("宋体", Font.BOLD, 14)));
        JOptionPane.showMessageDialog(null, "文件名填写错误或文件不存在，请检查格式！", "错误提醒", JOptionPane.ERROR_MESSAGE);
    }
    public void actionPerformed(ActionEvent e) {
        if(e.getSource() == windowTwo.comboBox2){
            i = windowTwo.comboBox2.getSelectedIndex() + 5;
//            System.out.println(i);
        }
//        储存分数
        if (e.getSource() == windowTwo.button1) {
            int count = Integer.parseInt(windowTwo.jTextField1.getText());
            if (Integer.parseInt(windowTwo.jTextField1.getText()) < 0 || Integer.parseInt(windowTwo.jTextField1.getText()) > 100) {
                initTwo();
                windowTwo.jTextField1.setText("");
            } else {
                //写进excel
                if (Objects.equals(windowTwo.comboBox1.getSelectedItem(), windowTwo.workbook.getSheetAt(0).getSheetName())) {
                    FileOutputStream fileOutput;
                    try {
//                        System.out.println(count);
                        windowTwo.workbook.getSheetAt(0).getRow(j).createCell(i).setCellValue(String.valueOf(count));
                        fileOutput = new FileOutputStream("F:\\java\\work\\JavaHomeWork\\src\\stu.xlsx");
                        windowTwo.workbook.write(fileOutput);
                        fileOutput.flush();
                        fileOutput.close();
                    } catch (IOException ex) {
                        System.out.println("储存出现错误！");
                    }
                } else if (Objects.equals(windowTwo.comboBox1.getSelectedItem(), windowTwo.workbook.getSheetAt(1).getSheetName())) {
                    FileOutputStream fileOutput;
                    try {
//                        System.out.println(count);
                        windowTwo.workbook.getSheetAt(1).getRow(j).createCell(i).setCellValue(String.valueOf(count));
                        fileOutput = new FileOutputStream("F:\\java\\work\\JavaHomeWork\\src\\stu.xlsx");
                        windowTwo.workbook.write(fileOutput);
                        fileOutput.flush();
                        fileOutput.close();
                    } catch (IOException ex) {
                        System.out.println("储存出现错误！");
                    }
               }
            }

            //写进word
            try {
                XSSFSheet sheet1 = windowTwo.workbook.getSheetAt(0);
                XSSFSheet sheet2 = windowTwo.workbook.getSheetAt(1);
                FileOutputStream fileOutputStream;
                XWPFDocument doc ;
                if (Objects.equals(windowTwo.comboBox1.getSelectedItem(), sheet1.getSheetName())) {
                    if (Objects.equals(WindowTwo.jTextField.getText(), "202210098100")) {
                        j = 4;
                    }
                    doc = new XWPFDocument(new FileInputStream("F:\\java\\work\\实验报告\\" + windowTwo.comboBox2.getSelectedItem()+ "\\" +sheet1.getSheetName() + ("\\" + sheet1.getRow(j).getCell(3) + sheet1.getRow(j).getCell(1) + ".docx")));
                    fileOutputStream = new FileOutputStream("F:\\java\\work\\实验报告\\" + windowTwo.comboBox2.getSelectedItem() + "\\" +sheet1.getSheetName() + ("\\" + sheet1.getRow(j).getCell(3) + sheet1.getRow(j).getCell(1) + ".docx"));
                } else {
                    if (Objects.equals(WindowTwo.jTextField.getText(), "202210098200")) {
                        j = 4;
                    }
                    doc = new XWPFDocument(new FileInputStream("F:\\java\\work\\实验报告\\"+ windowTwo.comboBox2.getSelectedItem()+ "\\" + sheet2.getSheetName() + ("\\" + sheet2.getRow(j).getCell(3) + sheet2.getRow(j).getCell(1) + ".docx")));
                    fileOutputStream = new FileOutputStream("F:\\java\\work\\实验报告\\"+ windowTwo.comboBox2.getSelectedItem()+ "\\" + sheet2.getSheetName() + ("\\" + sheet2.getRow(j).getCell(3) + sheet2.getRow(j).getCell(1) + ".docx"));
                }
                List<XWPFTable> tables = doc.getTables();
                tables.get(1).getRow(1).getCell(1).setColor("FF0000");
                tables.get(1).getRow(1).getCell(1).setText(String.valueOf(count));
                doc.write(fileOutputStream);
                fileOutputStream.flush();
                fileOutputStream.close();
                //判断文档中的姓名学号是否填写正确
                if (Objects.equals(windowTwo.workbook.getSheetAt(0).getRow(j).getCell(1).getStringCellValue(), tables.get(0).getRow(2).getCell(1).getText())) {
                    System.out.println("名字正确！");
                } else if (Objects.equals(windowTwo.workbook.getSheetAt(1).getRow(j).getCell(1).getStringCellValue(), tables.get(0).getRow(2).getCell(1).getText())) {
                    System.out.println("名字正确！");
                } else {
                    initThree();
                }
            } catch (IOException io) {
                initFour();
                System.out.println("实验报告读取错误！");
            }
        }

            //修改 j 的值
        if (e.getSource() == windowTwo.button2) {
            j++;
//            System.out.println(i+" "+j);
        }
        if (e.getSource() == windowTwo.button3) {
            j--;
//            System.out.println(i+" "+j);
        }
        if (e.getSource() == windowTwo.comboBox3) {
            j = windowTwo.comboBox3.getSelectedIndex() + 4;
//            System.out.println(i+" "+j);
        }
    }
}

